<style rel="stylesheet/scss" lang="scss" scoped>
</style>

<template>
    <div>
        <el-dialog title="关联属性" :visible.sync="relationVisible" :close-on-click-modal="false">
            <el-card shadow="hover" style="padding:0">

                <!-- 新建关联 -->
                <el-dialog
                    title="新建关联"
                    :visible.sync="visible" :close-on-click-modal="false" @close="handleDialogClose" append-to-body>
                    
                    <el-form ref="searchForm" :inline="true" :model="searchForm" size="medium" label-width="120px">
                        <el-card class="operate_container" shadow="never">
                            <div class="wrapper">
                                <!-- 关键字模糊检索 -->
                                <div class="searchKeyword">
                                    <el-form-item prop="keyword">
                                        <el-input placeholder="请输入检索关键字" v-model="searchForm.keyword" clearable @clear="handleResetSearch('searchForm')">
                                            <el-button slot="append" icon="el-icon-search" @click="handleSearch()"></el-button>
                                        </el-input>
                                    </el-form-item>
                                </div>
                            </div>
                        </el-card>
                    </el-form>

                    <!-- 表格 -->
                    <el-table
                        ref="table" size="small" empty-text="暂无数据"
                        element-loading-text="给我一点时间"
                        fit stripe border highlight-current-row
                        v-loading="dataListLoading"
                        :data="dataList"
                        :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                        @selection-change="handleSelection">
                        <el-table-column type="selection" width="55" align="center"></el-table-column>
                        <el-table-column type="index" width="55" align="center"></el-table-column>

                        <el-table-column label="属性名" prop="attrName" align="center"></el-table-column>
                        <el-table-column label="属性图标" prop="icon" align="center"></el-table-column>
                        <el-table-column label="可选值列表" prop="valueSelect" align="center"></el-table-column>
                    </el-table>

                    <!-- 分页工具栏 -->
                    <div class="pagination">
                        <el-pagination
                                @size-change="handleSizeChange"
                                @current-change="handleCurrentChange"
                                :current-page="searchForm.page"
                                :page-sizes="[10, 20, 30, 50, 100, 200]"
                                :page-size="searchForm.limit"
                                layout="total, sizes, prev, pager, next, jumper"
                                :total="totalPage">
                        </el-pagination>
                    </div>
                    
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="visible = false">取 消</el-button>
                        <el-button @click="handleSubmit('form')" type="primary" :loading="submitLoading">确认新增</el-button>
                    </div>
                </el-dialog>

                <el-card class="operate_container" shadow="never">
                    <div class="wrapper">
                        <div><i class="el-icon-tickets" style="margin-right: 10px;"></i>数据列表</div>
                        <!-- 操作按钮 -->
                        <div>
                            <el-button type="primary" icon="el-icon-plus" size="medium" circle @click="addOrUpdateVisible"></el-button>
                            <el-button :disabled="relationSelectionRows.length <= 0" type="danger" icon="el-icon-delete" size="medium" circle @click="relationRemove"></el-button>
                        </div>
                    </div>
                </el-card>
                <!-- 表格 -->
                <el-table
                    ref="table" size="small" empty-text="暂无数据"
                    element-loading-text="给我一点时间"
                    fit stripe border highlight-current-row
                    :data="relationAttrs"
                    :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                    @selection-change="handleRelationSelection">
                    <el-table-column type="selection" width="55" align="center"></el-table-column>

                    <el-table-column prop="attrName" label="属性名"></el-table-column>
                    <el-table-column prop="valueSelect" label="可选值">
                    <template slot-scope="scope">
                        <el-tooltip placement="top">
                            <div slot="content">
                                <span v-for="(i,index) in scope.row.valueSelect.split(';')" :key="index">{{i}}<br /></span>
                            </div>
                            <el-tag>{{scope.row.valueSelect.split(";")[0]+" ..."}}</el-tag>
                        </el-tooltip>
                    </template>
                    </el-table-column>

                    <el-table-column label="操作" width="120px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <div class="table_action">
                                <el-button type="danger" icon="el-icon-delete" circle size="small" @click="relationRemove(scope.row.id)"></el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>

            </el-card>
        </el-dialog>
    </div>
</template>

<script>

export default {
    data() {
        return {
            visible: false,
            relationVisible: false,
            searchVisible: false,

            dataList: [],
            totalPage: 0,
            submitLoading: false,
            dataListLoading: true,
            selectionRows: [],
            relationSelectionRows: [],

            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                keyword: null
            },

            relationAttrs: [],
            attrGroupId: 0
        }
    },

    methods: {
        /**
         * 打开窗口初始化数据
         */
		initUpdateRelationVisible(groupId) {
            this.relationVisible = true
            this.attrGroupId = groupId || 0;

            this.$http({
                url: '/product/pmsattrgroup/' + this.attrGroupId + '/attr/relation',
                method: "get"
            }).then(({ data }) => {
                this.relationAttrs = data;
            });
        },

        handleRelationSelection(selection) { // 多选
            this.relationSelectionRows = selection
        },
        
        /**
         * 移除关联
         */
        relationRemove(attrId) {
            let data = [];
            if(this.relationSelectionRows.length > 0) {
                this.relationSelectionRows.forEach(item => {
                    data.push({ attrId: item.id, attrGroupId: this.attrGroupId });
                });
            } else {
                data.push({ attrId, attrGroupId: this.attrGroupId });
            }
            
            this.$http({
                url: '/product/pmsattrgroup/attr/relation/delete',
                method: "post",
                data: data
            }).then((res) => {
                if (res.code === 200) {
                    this.$notify({ title: '提示', message: '删除成功', type: 'success' });
                    this.initUpdateRelationVisible(this.attrGroupId);
                } else {
                    this.$notify({ title: '错误', message: res.msg, type: 'error' });
                }
            });
        },
        
        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getDataList()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getDataList()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.handleReset(name)
            this.getDataList()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        handleSearch() { // 单条件搜索
            this.getDataList()
        },
        handleDialogClose() { //窗口关闭
        },

		/**
         * 打开表单窗口
         */
		addOrUpdateVisible(row) {
            this.visible = true
            this.getDataList()
        },


		/**
         * 提交表单
         */
        handleSubmit() {
            if (this.selectionRows.length > 0) {
                let postData = [];
                this.selectionRows.forEach(item => {
                    postData.push({ attrId: item.id, attrGroupId: this.attrGroupId });
                });
                this.$http({
                    url: '/product/pmsattrgroup/attr/relation',
                    method: "post",
                    data: postData
                }).then(res => {
                    if (this.ResultMessage(res)) {
                        this.visible = false
                        this.getDataList()
                    }
                    this.$emit("refreshData");
                    this.initUpdateRelationVisible(this.attrGroupId);
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                });
            }
        },
		
        /**
         * 数据列表
         */
        getDataList() {
            this.$http({
                url: '/product/pmsattrgroup/'+ this.attrGroupId +'/noattr/relation',
                method: "get",
                params: this.searchForm
            }).then((res) => {
                if (this.ResultMessage(res, false)) {
                    this.dataList = res.data.records
                    this.totalPage = res.data.total
                    this.dataListLoading = false
                } else {
                    this.dataListLoading = false
                }
            }).catch(error => {
                this.dataListLoading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            });
        }
    }
}
</script>